package com.bsdk.install.device.lockLora.v1;


import com.alibaba.fastjson2.JSONObject;
import com.bsdk.common.core.domain.AjaxResult;
import com.bsdk.common.utils.SpringContextUtil;
import com.bsdk.install.device.IProductId;
import com.bsdk.install.device.LockBase;
import com.bsdk.install.device.constants.LockConstants;
import com.bsdk.install.device.lockLora.LockLora;
import com.bsdk.install.device.lockLora.v1.cmd.ANALYSIS;
import com.bsdk.install.device.lockLora.v1.cmd.COMMAND;
import com.bsdk.install.queue.PublicQueue;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class DeviceCmd extends LockBase  implements LockLora, IProductId {

    private JSONObject deviceCommand = new JSONObject();

    public DeviceCmd(String devId, String mac) {

        this.setcMac(mac);
        this.setDevId(devId);
        this.deviceCommand.put("devId", this.getDevId());
        this.deviceCommand.put("cMac", this.getcMac());

        this.deviceCommand.put("uId", String.valueOf(LockConstants.COUNTER.getAndIncrement()));

    }

    public DeviceCmd() {
    }
    @Override
    public AjaxResult commandPingLock(String devId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "00010A");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.pinglock());
        return AjaxResult.success(this.deviceCommand);

    }
    @Override
    public AjaxResult commandRegistLock(String devId)
    {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "00010A");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.registlock());
        return AjaxResult.success(this.deviceCommand);
    }


    @Override
    public AjaxResult commandOpenRemote() {
        //指令转换

        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "00014A");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.unlock());
        return AjaxResult.success(this.deviceCommand);

    }
    @Override
    public AjaxResult commandCloseRemote() {
        //指令转换

        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "00014A");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.lock());
        return AjaxResult.success(this.deviceCommand);

    }
    @Override
    public AjaxResult commandReset() {
        //指令转换

        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "00014A");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.reset());
        return AjaxResult.success(this.deviceCommand);

    }

    @Override
    public AjaxResult commandSyncTime() {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0003AH");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.adjustTime());
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandAddUser(boolean admin,String pwType, String pw, Date startTime, Date endTime, int userId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.addOrModifyUserPassword(admin,pwType, pw, startTime, endTime, userId));
        return AjaxResult.success(this.deviceCommand);
    }
    @Override
    public AjaxResult commandAddUser(boolean admin,String pwType, String pw, Date startTime, Date endTime, int userId,JSONObject cycle,int times) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.addOrModifyUserPassword(admin,pwType, pw, startTime, endTime, userId,cycle,times));
        return AjaxResult.success(this.deviceCommand);
    }
    @Override
    public AjaxResult commandActiveOrDisableUser(boolean active, int userId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.frozenOrUnFrozoneByUserId(active, userId));
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandEditPasswordById(String pw, int userId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.modifyPasswordByUserId(userId, pw));
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandAddKey(String key) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.addAdminPassword(key));
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandEditExpireTimeById(Date startTime, Date endTime, int userId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.modifierValidTime(userId, startTime, endTime));
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandDeleteUserById(int userId) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        this.deviceCommand.put("cmd", "0220c0");
        COMMAND cmd = new COMMAND();
        this.deviceCommand.put("data", cmd.DeleteUserById(userId));
        return AjaxResult.success(this.deviceCommand);
    }

    @Override
    public AjaxResult commandGetBriefPassword(Date startTime, Integer duration, String key) {
        this.deviceCommand.put("cMac", "8000"+this.getcMac().substring(4));
        COMMAND cmd = new COMMAND();

        return AjaxResult.success(cmd.generalOffLinePassword(startTime, duration, key));
    }

    @Override
    public JSONObject result(JSONObject jsonObject) {

        return (new ANALYSIS()).dealJsonData(jsonObject);

    }

    /**
     * 放入mqtt推送队列中
     *
     * @param jsonObject
     * @return
     */
    @Override
    public AjaxResult send(JSONObject jsonObject) {

        SpringContextUtil.getBean(PublicQueue.class).setDown(jsonObject);

        if (PublicQueue.MAPRESULTQUEUE.containsKey(jsonObject.getString("uId"))) {
            JSONObject jsonObject1 = null;
            try {
                jsonObject1 = (JSONObject) PublicQueue.MAPRESULTQUEUE.get(jsonObject.getString("uId")).take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return AjaxResult.success(jsonObject1);

        } else {
            return AjaxResult.error("结果队列不存在");
        }

    }

}
